
###############################################################################
## CuteTimer
## Speed-run plugin for EventScripts Python
## Officially supported games: CS:S OB
##
## File: core/interface.py
## Author: zoomj
## Description: global data and object instance share system 
## Version: 0.1

__all__ = [
    'interface'
]

class InterfaceManager(object):
    iface = None
    
    def __init__(self):
        pass
        
    def __setattr__(self, name, value):
        if name == 'iface':
            if hasattr(value, '__name__'):
                if value.__name__ == 'iface_list':
                    super(InterfaceManager, self).__setattr__(name, value)
                    return
        raise TypeError('interface assigned with non-interface type data')
        
    def __delattr__(self, name):
        if name == 'iface':
            raise TypeError('interface object cannot be deleted')
        
    def iface_create(self, name, instance):
        if not self.iface:
            self.iface = type('iface_list', (), { name : instance })
            return True
        else:
            if hasattr(self.iface, name):
                return False
            self.iface = type('iface_list', (),
               dict({ name : instance }.items() + self.iface.__dict__.items()))
            return True
        return True
        
    def iface_exists(self, name):
        return hasattr(self.iface, name)

interface = InterfaceManager()
